﻿using System;
using System.Collections.Generic;
using System.Text;
using PersistenceObject.Framework.Expressions;
using System.Data;
using PersistenceObject.Framework.Interface;

namespace PersistenceObject.Framework
{
    public class DataOperate
    {
        public static DataOperate Create()
        {
            return new DataOperate();
        }

        public static DataOperate Create(String sessionKey)
        {
            return new DataOperate(sessionKey);
        }

        public DataOperate() { }

        public DataOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        public Int32 ExecuteStoredProcedure(String storedName)
        {
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                session.Open();
                IDbCommand comm = session.DbDriver.GetCommand();
                comm.CommandText = storedName;
                comm.CommandType = CommandType.StoredProcedure;
                return session.ExecuteNonQuery(comm);
            }
        }

        public Int32 ExecuteSql(String sql)
        {
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                session.Open();
                IDbCommand comm = session.DbDriver.GetCommand();
                comm.CommandText = sql;
                comm.CommandType = CommandType.Text;
                return session.ExecuteNonQuery(comm);
            }
        }

        public Int32 ExecuteStoredProcedure(String storedName, IDataSession session)
        {

            IDbCommand comm = session.DbDriver.GetCommand();
            comm.CommandText = storedName;
            comm.CommandType = CommandType.StoredProcedure;
            return session.ExecuteNonQuery(comm);

        }

        public Int32 ExecuteSql(String sql, IDataSession session)
        {
            IDbCommand comm = session.DbDriver.GetCommand();
            comm.CommandText = sql;
            comm.CommandType = CommandType.Text;
            return session.ExecuteNonQuery(comm);

        }

    }


    public class InsertOperate
    {
        public static InsertOperate Create()
        {
            return new InsertOperate();
        }

        public static InsertOperate Create(String sessionKey)
        {
            return new InsertOperate(sessionKey);
        }

        public static InsertOperate Create<T>() where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            InsertOperate io = new InsertOperate();
            io.table = om.Table;

            return io;
        }

        public static InsertOperate Create<T>(String sessionKey) where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            InsertOperate io = new InsertOperate(sessionKey);
            io.table = om.Table;

            return io;
        }

        public InsertOperate() { }

        public InsertOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        private FieldValue[] fields;

        public InsertOperate Fields(params FieldValue[] fields)
        {
            this.fields = fields;
            return this;
        }

        private Table table;

        public InsertOperate From(Table table)
        {
            this.table = table;
            return this;
        }

        public void Execute<T>(T t) where T : class
        {
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                session.Open();
                session.Insert<T>(t);
            }
        }

        public void Execute<T>() where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IInsert insert = session.CreateInsert(table);
                insert.Fields = fields;
                session.Open();
                insert.Execute();
            }
        }

        public void Execute()
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IInsert insert = session.CreateInsert(table);
                insert.Fields = fields;
                session.Open();
                insert.Execute();
            }
        }

        public void Execute<T>(T t, IDataSession session) where T : class
        {
            session.Insert<T>(t);
        }

        public void Execute<T>(IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IInsert insert = session.CreateInsert(table);
            insert.Fields = fields;
            insert.Execute();
        }

        public void Execute(IDataSession session)
        {

            IInsert insert = session.CreateInsert(table);
            insert.Fields = fields;
            insert.Execute();
        }

    }

    public class InsertOperate<T> where T : class
    {
        private IDataSession _Session;

        public static InsertOperate<T> Create()
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            InsertOperate<T> io = new InsertOperate<T>();
            io.table = om.Table;

            return io;

        }

        public static InsertOperate<T> Create(IDataSession session)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            InsertOperate<T> io = new InsertOperate<T>();
            io.table = om.Table;
            io._Session = session;

            return io;

        }

        public static InsertOperate<T> Create(String sessionKey)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            InsertOperate<T> io = new InsertOperate<T>(sessionKey);
            io.table = om.Table;

            return io;
        }

        public InsertOperate() { }

        public InsertOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        private FieldValue[] fields;

        public InsertOperate<T> Fields(params FieldValue[] fields)
        {
            this.fields = fields;
            return this;
        }

        private Table table;

        private Table fromTable;

        public InsertOperate<T> From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        public InsertOperate<T> From(string tableName)
        {
            this.fromTable = new Table(tableName);
            return this;
        }

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        public void Execute<TR>(TR t) where TR : class
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    session.Insert<TR>(t);
                }
            }
            else
            {
                _Session.Insert<TR>(t);
            }
        }

        public void Execute<TR>() where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IInsert insert = session.CreateInsert(Table);
                    insert.Fields = fields;
                    session.Open();
                    insert.Execute();
                }
            }
            else
            {
                IInsert insert = _Session.CreateInsert(Table);
                insert.Fields = fields;
                insert.Execute();
            }
        }

        public void Execute()
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IInsert insert = session.CreateInsert(Table);
                    insert.Fields = fields;
                    session.Open();
                    insert.Execute();
                }
            }
            else
            {
                IInsert insert = _Session.CreateInsert(Table);
                insert.Fields = fields;
                insert.Execute();
            }
        }

        public void Execute(T t)
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    session.Insert<T>(t, this.Table);
                }
            }
            else
            {
                _Session.Insert<T>(t, this.Table);
            }
        }

        public void Execute(T t,Boolean isGetAutoID)
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    if (isGetAutoID)
                    {
                        session.Insert<T>(t, this.Table, isGetAutoID);
                    }
                    else { session.Insert<T>(t, this.Table); }
                }
            }
            else
            {
                if (isGetAutoID)
                {
                    _Session.Insert<T>(t, this.Table, isGetAutoID);
                }
                else
                {
                    _Session.Insert<T>(t, this.Table);
                }
            }
        }

        public void Execute<TR>(TR t, IDataSession session) where TR : class
        {
            session.Insert<TR>(t);
        }

        public void Execute<TR>(IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IInsert insert = session.CreateInsert(Table);
            insert.Fields = fields;
            insert.Execute();
        }

        public void Execute(IDataSession session)
        {

            IInsert insert = session.CreateInsert(Table);
            insert.Fields = fields;
            insert.Execute();
        }

        public void Execute(T t, IDataSession session)
        {
            session.Insert<T>(t);
        }

    }

    public class UpdateOperate
    {
        public static UpdateOperate Create()
        {
            return new UpdateOperate();
        }

        public static UpdateOperate Create(String sessionKey)
        {
            return new UpdateOperate(sessionKey);
        }

        public static UpdateOperate Create<T>() where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            UpdateOperate uo = new UpdateOperate();
            uo.table = om.Table;

            return uo;
        }

        public static UpdateOperate Create<T>(String sessionKey) where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            UpdateOperate uo = new UpdateOperate(sessionKey);
            uo.table = om.Table;

            return uo;
        }

        public UpdateOperate() { }

        public UpdateOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        private FieldValue[] fields;

        public UpdateOperate Fields(params FieldValue[] fields)
        {
            this.fields = fields;
            return this;
        }

        private Table table;

        public UpdateOperate From(Table table)
        {
            this.table = table;
            return this;
        }

        private Expression expression;

        public UpdateOperate Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        public Int32 Execute<T>(T t) where T : class
        {
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                session.Open();
                return session.Update<T>(t);
            }
        }

        public Int32 Execute()
        {
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IUpdate update = session.CreateUpdate(table);
                update.Fields = fields;
                update.Expressions = expression;
                session.Open();
                return update.Execute();
            }
        }

        public Int32 Execute<T>(T t, IDataSession session) where T : class
        {
            return session.Update<T>(t);
        }

        public Int32 Execute(IDataSession session)
        {
            IUpdate update = session.CreateUpdate(table);
            update.Fields = fields;
            update.Expressions = expression;
            return update.Execute();
        }

    }

    public class UpdateOperate<T> where T : class
    {

        private IDataSession _Session;
        public static UpdateOperate<T> Create()
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            UpdateOperate<T> uo = new UpdateOperate<T>();
            uo.table = om.Table;

            return uo;
        }

        public static UpdateOperate<T> Create(IDataSession session)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            UpdateOperate<T> uo = new UpdateOperate<T>();
            uo.table = om.Table;
            uo._Session = session;
            return uo;
        }

        public static UpdateOperate<T> Create(String sessionKey)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            UpdateOperate<T> uo = new UpdateOperate<T>(sessionKey);
            uo.table = om.Table;

            return uo;
        }

        public UpdateOperate() { }

        public UpdateOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        private FieldValue[] fields;

        public UpdateOperate<T> Fields(params FieldValue[] fields)
        {
            this.fields = fields;
            return this;
        }

        private Table table;

        private Table fromTable;

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        public UpdateOperate<T> From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        public UpdateOperate<T> From(string tableName)
        {
            this.fromTable = new Table(tableName);
            return this;
        }

        private Expression expression;

        public UpdateOperate<T> Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        public Int32 Execute<TR>(TR t) where TR : class
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    return session.Update<TR>(t);
                }
            }
            else
            {
                return _Session.Update<TR>(t);
            }
        }

        public Int32 Execute(T t)
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    return session.Update<T>(t, this.Table);
                }
            }
            else
            {
                return _Session.Update<T>(t, this.Table);
            }
        }

        public Int32 Execute()
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IUpdate update = session.CreateUpdate(Table);
                    update.Fields = fields;
                    update.Expressions = expression;
                    session.Open();
                    return update.Execute();
                }
            }
            else
            {
                IUpdate update = _Session.CreateUpdate(Table);
                update.Fields = fields;
                update.Expressions = expression;
                return update.Execute();
            }
        }

        public Int32 Execute<TR>(TR t, IDataSession session) where TR : class
        {
            return session.Update<TR>(t);
        }

        public Int32 Execute(IDataSession session)
        {
            IUpdate update = session.CreateUpdate(Table);
            update.Fields = fields;
            update.Expressions = expression;
            return update.Execute();
        }

        public Int32 Execute(T t, IDataSession session)
        {
            return session.Update<T>(t);

        }

    }

    public class DeleteOperate
    {
        public static DeleteOperate Create()
        {
            return new DeleteOperate();
        }

        public static DeleteOperate Create(String sessionKey)
        {
            return new DeleteOperate(sessionKey);
        }


        public static DeleteOperate Create<T>() where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            DeleteOperate delo = new DeleteOperate();
            delo.table = om.Table;

            return delo;
        }

        public static DeleteOperate Create<T>(String sessionKey) where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            DeleteOperate delo = new DeleteOperate(sessionKey);
            delo.table = om.Table;

            return delo;
        }

        public DeleteOperate() { }

        public DeleteOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        private Table table;

        public DeleteOperate From(Table table)
        {
            this.table = table;
            return this;
        }

        private Expression expression;

        public DeleteOperate Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        public void Execute<T>(T t) where T : class
        {
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                session.Open();
                session.Delete<T>(t);
            }
        }


        public Int32 Execute<T>() where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;

            using (IDataSession session = new DataSession(this._SessionKey))
            {

                IDelete delete = session.CreateDelete(table);
                delete.Expressions = expression;
                session.Open();
                return delete.Execute();
            }
        }

        public Int32 Execute()
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {

                IDelete delete = session.CreateDelete(table);
                delete.Expressions = expression;
                session.Open();
                return delete.Execute();
            }
        }

        public void Execute<T>(T t, IDataSession session) where T : class
        {
            session.Delete<T>(t);
        }

        public Int32 Execute<T>(IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IDelete delete = session.CreateDelete(table);
            delete.Expressions = expression;
            return delete.Execute();
        }

        public Int32 Execute(IDataSession session)
        {

            IDelete delete = session.CreateDelete(table);
            delete.Expressions = expression;
            return delete.Execute();
        }

    }

    public class DeleteOperate<T> where T : class
    {
        private IDataSession _Session;

        public static DeleteOperate<T> Create()
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            DeleteOperate<T> delo = new DeleteOperate<T>();
            delo.table = om.Table;

            return delo;
        }

        public static DeleteOperate<T> Create(IDataSession session)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            DeleteOperate<T> delo = new DeleteOperate<T>();
            delo.table = om.Table;
            delo._Session = session;
            

            return delo;
        }

        public static DeleteOperate<T> Create(String sessionKey)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));

            DeleteOperate<T> delo = new DeleteOperate<T>(sessionKey);
            delo.table = om.Table;

            return delo;
        }


        public DeleteOperate() { }

        public DeleteOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }

        private Table table;

        public DeleteOperate<T> From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        public DeleteOperate<T> From(string tableName)
        {
            this.fromTable = new Table(tableName);
            return this;
        }

        private Table fromTable;

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        private Expression expression;

        public DeleteOperate<T> Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        public void Execute<TR>(TR t) where TR : class
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    session.Delete<TR>(t);
                }
            }
            else
            {
                _Session.Delete<TR>(t);
            }
        }


        public Int32 Execute<TR>() where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {


                using (IDataSession session = new DataSession(this._SessionKey))
                {

                    IDelete delete = session.CreateDelete(Table);
                    delete.Expressions = expression;
                    session.Open();
                    return delete.Execute();
                }
            }
            else
            {
                IDelete delete = _Session.CreateDelete(Table);
                delete.Expressions = expression;
                return delete.Execute();
            }
        }

        public Int32 Execute()
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {

                    IDelete delete = session.CreateDelete(Table);
                    delete.Expressions = expression;
                    session.Open();
                    return delete.Execute();
                }
            }
            else
            {
                IDelete delete = _Session.CreateDelete(Table);
                delete.Expressions = expression;
                return delete.Execute();
            }
        }

        public void Execute(T t)
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    session.Open();
                    session.Delete<T>(t, Table);
                }
            }
            else
            {
                _Session.Delete<T>(t, Table);
            }
        }

        public void Execute<TR>(TR t, IDataSession session) where TR : class
        {
            session.Delete<TR>(t);
        }

        public Int32 Execute<TR>(IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IDelete delete = session.CreateDelete(Table);
            delete.Expressions = expression;
            return delete.Execute();
        }

        public Int32 Execute(IDataSession session)
        {

            IDelete delete = session.CreateDelete(Table);
            delete.Expressions = expression;
            return delete.Execute();
        }

        public void Execute(T t, IDataSession session)
        {
            session.Delete<T>(t);

        }

    }

    public class QueryOperate
    {
        public static QueryOperate Create()
        {
            return new QueryOperate();
        }

        public static QueryOperate Create(String sessionKey)
        {
            return new QueryOperate(sessionKey);
        }

        public QueryOperate() { }

        public QueryOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }


        private Table table;

        private Table fromTable;

        public QueryOperate From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        private Expression expression;

        public QueryOperate Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        private FieldAdapter[] select;

        public QueryOperate Select(params FieldAdapter[] select)
        {
            this.select = select;
            return this;
        }

        private FieldAdapter[] orderBy;

        public QueryOperate OrderBy(params FieldAdapter[] orderBy)
        {
            this.orderBy = orderBy;
            return this;
        }

        private FieldAdapter[] groupBy;

        public QueryOperate GroupBy(params FieldAdapter[] groupBy)
        {
            this.groupBy = groupBy;
            return this;
        }

        private Int32 top;

        public QueryOperate Top(Int32 top)
        {
            this.top = top;
            return this;
        }

        private Int32 size;

        public QueryOperate Page(Int32 size)
        {
            this.size = size;
            return this;
        }

        /// <summary>
        /// 返回最大值
        /// </summary>
        /// <typeparam name="TR">返回值类型</typeparam>
        /// <typeparam name="TT">对应到那个实体类型</typeparam>
        /// <returns></returns>
        public TR Max<TR, TT>(FieldAdapter field)
            where TR : struct
            where TT : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TT));
            this.table = om.Table;

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;

                query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create(String.Format("Max({0})",field)) 
                };
                session.Open();
                Object o = null;
                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        o = dr.GetValue(0);
                    }
                }

                if (o == null) return default(TR);

                return (TR)o;
            }


        }

        public TR Min<TR, TT>(FieldAdapter field)
            where TR : struct
            where TT : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TT));
            this.table = om.Table;
            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;

                query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create(String.Format("Min({0})",field)) 
                };
                session.Open();
                Object o = null;
                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        o = dr.GetValue(0);
                    }
                }

                if (o == null) return default(TR);

                return (TR)o;
            }


        }

        public T Get<T>() where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                session.Open();
                return query.Get<T>();
            }
        }

        public T Get<T>(String sql) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.Get<T>(sql);
            }
        }

        public T Get<T>(String commandText, CommandType commandType) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.Get<T>(commandText, commandType);
            }
        }

        public IList<T> List<T>() where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                session.Open();
                return query.List<T>();
            }
        }

        public IList<T> List<T>(String sql) where T : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.List<T>(sql);
            }
        }

        public IList<T> List<T>(String commandText, CommandType commandType) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.List<T>(commandText, commandType);
            }
        }

        public Int32 GetCount<T>() where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                IPager pager = query.CreatePager();
                session.Open();
                return pager.GetCount();
            }
        }

        public IList<T> List<T>(Int32 pageIndex) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                IPager pager = query.CreatePager(this.size);
                session.Open();
                return pager.List<T>(pageIndex);
            }
        }


        public Boolean Exist<T>() where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create("Count(*)")
                };
                session.Open();
                Int32 o = (Int32)query.ExecuteScalar();
                if (o > 0) return true;
                return false;

            }
        }

        public Boolean Exist<T>(IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;



            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.Selects = new FieldAdapter[]{
                FieldAdapter.Create("Count(*)")
            };
            Int32 o = (Int32)query.ExecuteScalar();
            if (o > 0) return true;
            return false;

        }


        public T Get<T>(IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;
            return query.Get<T>();
        }

        public T Get<T>(String sql, IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            return query.Get<T>(sql);

        }

        public T Get<T>(String commandText, CommandType commandType, IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);

            return query.Get<T>(commandText, commandType);

        }



        public IList<T> List<T>(IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;

            return query.List<T>();

        }

        public IList<T> List<T>(String sql, IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);

            return query.List<T>(sql);

        }

        public IList<T> List<T>(String commandText, CommandType commandType, IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);

            return query.List<T>(commandText, commandType);

        }

        public Int32 GetCount<T>(IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.OrderBy = orderBy;
            IPager pager = query.CreatePager();
            return pager.GetCount();
        }

        public IList<T> List<T>(Int32 pageIndex, IDataSession session) where T : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            IPager pager = query.CreatePager(this.size);
            return pager.List<T>(pageIndex);

        }

    }

    public class QueryOperate<T> where T : class
    {
        private IDataSession _Session;
        public static QueryOperate<T> Create()
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            QueryOperate<T> qo = new QueryOperate<T>();
            qo.table = om.Table;

            return qo;
        }

        public static QueryOperate<T> Create(IDataSession session)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            QueryOperate<T> qo = new QueryOperate<T>();
            qo.table = om.Table;
            qo._Session = session;

            return qo;
        }

        public static QueryOperate<T> Create(String sessionKey)
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(T));
            QueryOperate<T> qo = new QueryOperate<T>(sessionKey);
            qo.table = om.Table;

            return qo;
        }


        public QueryOperate() { }

        public QueryOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }


        private Table table;

        private Table fromTable;

        public QueryOperate<T> From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        public QueryOperate<T> From(string tableName)
        {
            this.fromTable = new Table(tableName);
            return this;
        }

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        private Expression expression;

        public QueryOperate<T> Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        private FieldAdapter[] select;

        public QueryOperate<T> Select(params FieldAdapter[] select)
        {
            this.select = select;
            return this;
        }

        private FieldAdapter[] orderBy;

        public QueryOperate<T> OrderBy(params FieldAdapter[] orderBy)
        {
            this.orderBy = orderBy;
            return this;
        }

        private FieldAdapter[] groupBy;

        public QueryOperate<T> GroupBy(params FieldAdapter[] groupBy)
        {
            this.groupBy = groupBy;
            return this;
        }

        private Int32 top;

        public QueryOperate<T> Top(Int32 top)
        {
            this.top = top;
            return this;
        }

        private Int32 size;

        public QueryOperate<T> Page(Int32 size)
        {
            this.size = size;
            return this;
        }

        /// <summary>
        /// 返回最大值
        /// </summary>
        /// <typeparam name="TR">返回值类型</typeparam>
        /// <returns></returns>
        public TR Max<TR>(FieldAdapter field)
            where TR : struct
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;

                    query.Selects = new FieldAdapter[]{
                        FieldAdapter.Create(String.Format("Max({0})",field)) 
                    };
                    session.Open();
                    Object o = null;
                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            o = dr.GetValue(0);
                        }
                    }


                    if (o == null || o.GetType() == typeof(System.DBNull)) return default(TR);

                    //if(o.GetType()

                    return (TR)o;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;

                query.Selects = new FieldAdapter[]{
                        FieldAdapter.Create(String.Format("Max({0})",field)) 
                    };

                Object o = null;
                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        o = dr.GetValue(0);
                    }
                }


                if (o == null || o.GetType() == typeof(System.DBNull)) return default(TR);

                //if(o.GetType()

                return (TR)o;
            }


        }

        public TR Min<TR>(FieldAdapter field)
            where TR : struct
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;

                    query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create(String.Format("Min({0})",field)) 
                };
                    session.Open();
                    Object o = null;
                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            o = dr.GetValue(0);
                        }
                    }

                    if (o == null || o.GetType() == typeof(System.DBNull)) return default(TR);


                    return (TR)o;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;

                query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create(String.Format("Min({0})",field)) 
                };
                Object o = null;
                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        o = dr.GetValue(0);
                    }
                }

                if (o == null || o.GetType() == typeof(System.DBNull)) return default(TR);


                return (TR)o;
            }

        }


        public T Get()
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = select;
                    query.Top = top;
                    session.Open();
                    return query.Get<T>();
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                return query.Get<T>();
            }
        }

        public T Get(String sql)
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.Get<T>(sql);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                return query.Get<T>(sql);
            }
        }

        public T Get(String commandText, CommandType commandType)
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.Get<T>(commandText, commandType);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                return query.Get<T>(commandText, commandType);
            }
        }

        public IList<T> List()
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = select;
                    query.Top = top;
                    session.Open();
                    return query.List<T>();
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                return query.List<T>();
            }
        }

        #region 提取固定列表

        public IList<Int32> ListInt32(IntField field)
        {
            IList<Int32> tfs = new List<Int32>();
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetInt32(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetInt32(0));
                    }
                }

                return tfs;
            }
        }

        public IList<Boolean> ListBoolean(BooleanField field)
        {
            IList<Boolean> tfs = new List<Boolean>();
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetBoolean(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetBoolean(0));
                    }
                }

                return tfs;
            }
        }


        public IList<DateTime> ListDateTime(DateTimeField field)
        {
            IList<DateTime> tfs = new List<DateTime>();
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetDateTime(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetDateTime(0));
                    }
                }

                return tfs;
            }
        }

        public IList<Decimal> ListDecimal(DecimalField field)
        {
            IList<Decimal> tfs = new List<Decimal>();
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetDecimal(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetDecimal(0));
                    }
                }

                return tfs;
            }
        }

        public IList<Double> ListDouble(DoubleField field)
        {
            IList<Double> tfs = new List<Double>();
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetDouble(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetDouble(0));
                    }
                }

                return tfs;
            }
        }

        public IList<Guid> ListGuid(GuidField field)
        {
            IList<Guid> tfs = new List<Guid>();
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetGuid(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetGuid(0));
                    }
                }

                return tfs;
            }
        }

        public IList<Object> ListObject(ObjectField field)
        {
            IList<Object> tfs = new List<Object>();
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetValue(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetValue(0));
                    }
                }

                return tfs;
            }
        }

        public IList<String> ListString(StringField field)
        {
            IList<String> tfs = new List<String>();
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            tfs.Add(dr.GetString(0));
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        tfs.Add(dr.GetString(0));
                    }
                }

                return tfs;
            }
        }

        #endregion

        #region 提取固定列表

        public Int32 GetInt32(IntField field)
        {
            Int32 tfs = 0;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetInt32(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetInt32(0);
                    }
                }

                return tfs;
            }
        }

        public Boolean GetBoolean(BooleanField field)
        {
            Boolean tfs = false;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetBoolean(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetBoolean(0);
                    }
                }

                return tfs;
            }
        }


        public DateTime GetDateTime(DateTimeField field)
        {
            DateTime tfs = DateTime.MinValue;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetDateTime(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetDateTime(0);
                    }
                }

                return tfs;
            }
        }

        public Decimal GetDecimal(DecimalField field)
        {
            Decimal tfs = 0;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetDecimal(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetDecimal(0);
                    }
                }

                return tfs;
            }
        }

        public Double GetDouble(DoubleField field)
        {
            Double tfs = 0;
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetDouble(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetDouble(0);
                    }
                }

                return tfs;
            }
        }

        public Guid GetGuid(GuidField field)
        {
            Guid tfs = Guid.Empty;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetGuid(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetGuid(0);
                    }
                }

                return tfs;

            }
        }

        public Object GetObject(ObjectField field)
        {
            Object tfs = null;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            tfs = dr.GetValue(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        tfs = dr.GetValue(0);
                    }
                }

                return tfs;
            }
        }

        public String GetString(StringField field)
        {
            String tfs = null;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = new FieldAdapter[] { field };
                    query.Top = top;
                    session.Open();

                    using (IDataReader dr = query.ExecuteReader())
                    {
                        if (dr.Read())
                        {

                            tfs = dr.GetString(0);
                        }
                    }

                    return tfs;
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = new FieldAdapter[] { field };
                query.Top = top;

                using (IDataReader dr = query.ExecuteReader())
                {
                    if (dr.Read())
                    {

                        tfs = dr.GetString(0);
                    }
                }

                return tfs;
            }
        }

        #endregion


        public IList<T> List(String sql)
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.List<T>(sql);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);

                return query.List<T>(sql);
            }
        }

        public IList<T> List(String commandText, CommandType commandType)
        {
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.List<T>(commandText, commandType);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                return query.List<T>(commandText, commandType);
            }
        }

        public Int32 GetCount()
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    IPager pager = query.CreatePager();
                    session.Open();
                    return pager.GetCount();
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                IPager pager = query.CreatePager();
                return pager.GetCount();
            }
        }

        public IList<T> List(Int32 pageIndex)
        {
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = select;
                    IPager pager = query.CreatePager(this.size);
                    session.Open();
                    return pager.List<T>(pageIndex);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                IPager pager = query.CreatePager(this.size);
                return pager.List<T>(pageIndex);
            }
        }


        public Boolean Exist()
        {
            if (_Session == null)
            {


                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.Selects = new FieldAdapter[]{
                        FieldAdapter.Create("Count(*)")
                    };
                    session.Open();
                    Int32 o = (Int32)query.ExecuteScalar();
                    if (o > 0) return true;
                    return false;

                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.Selects = new FieldAdapter[]{
                        FieldAdapter.Create("Count(*)")
                    };
                Int32 o = (Int32)query.ExecuteScalar();
                if (o > 0) return true;
                return false;
            }
        }

        public Boolean Exist(IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.Selects = new FieldAdapter[]{
                FieldAdapter.Create("Count(*)")
            };
            Int32 o = (Int32)query.ExecuteScalar();
            if (o > 0) return true;
            return false;

        }


        public T Get(IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;
            return query.Get<T>();
        }

        public T Get(String sql, IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            return query.Get<T>(sql);

        }

        public T Get(String commandText, CommandType commandType, IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);

            return query.Get<T>(commandText, commandType);

        }



        public IList<T> List(IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;

            return query.List<T>();

        }

        public IList<T> List(String sql, IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);

            return query.List<T>(sql);

        }

        public IList<T> List(String commandText, CommandType commandType, IDataSession session)
        {



            IQuery query = session.CreateQuery(Table);

            return query.List<T>(commandText, commandType);

        }

        public Int32 GetCount(IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.OrderBy = orderBy;
            IPager pager = query.CreatePager();
            return pager.GetCount();
        }

        public IList<T> List(Int32 pageIndex, IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            IPager pager = query.CreatePager(this.size);
            return pager.List<T>(pageIndex);

        }


        public TR Get<TR>() where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;
            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = select;
                    query.Top = top;
                    session.Open();
                    return query.Get<TR>();
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                return query.Get<TR>();
            }
        }

        public TR Get<TR>(String sql) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.Get<TR>(sql);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                return query.Get<TR>(sql);
            }
        }

        public TR Get<TR>(String commandText, CommandType commandType) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.Get<TR>(commandText, commandType);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                return query.Get<TR>(commandText, commandType);
            }
        }

        public IList<TR> List<TR>() where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;
            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = select;
                    query.Top = top;
                    session.Open();
                    return query.List<TR>();
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                return query.List<TR>();
            }
        }

        public IList<TR> List<TR>(String sql) where TR : class
        {
            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.List<TR>(sql);
                }
            }
            else
            {

                IQuery query = _Session.CreateQuery(Table);
                return query.List<TR>(sql);
            }
        }

        public IList<TR> List<TR>(String commandText, CommandType commandType) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {

                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    session.Open();
                    return query.List<TR>(commandText, commandType);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                return query.List<TR>(commandText, commandType);
            }
        }

        public Int32 GetCount<TR>() where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    IPager pager = query.CreatePager();
                    session.Open();
                    return pager.GetCount();
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                IPager pager = query.CreatePager();
                return pager.GetCount();
            }
        }

        public IList<TR> List<TR>(Int32 pageIndex) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.GroupBy = groupBy;
                    query.OrderBy = orderBy;
                    query.Selects = select;
                    IPager pager = query.CreatePager(this.size);
                    session.Open();
                    return pager.List<TR>(pageIndex);
                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                IPager pager = query.CreatePager(this.size);
                return pager.List<TR>(pageIndex);
            }
        }


        public Boolean Exist<TR>() where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;

            if (_Session == null)
            {
                using (IDataSession session = new DataSession(this._SessionKey))
                {
                    IQuery query = session.CreateQuery(Table);
                    query.Expressions = expression;
                    query.Selects = new FieldAdapter[]{
                            FieldAdapter.Create("Count(*)")
                        };
                    session.Open();
                    Int32 o = (Int32)query.ExecuteScalar();
                    if (o > 0) return true;
                    return false;

                }
            }
            else
            {
                IQuery query = _Session.CreateQuery(Table);
                query.Expressions = expression;
                query.Selects = new FieldAdapter[]{
                            FieldAdapter.Create("Count(*)")
                        };
                Int32 o = (Int32)query.ExecuteScalar();
                if (o > 0) return true;
                return false;
            }
        }

        public Boolean Exist<TR>(IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;



            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.Selects = new FieldAdapter[]{
                FieldAdapter.Create("Count(*)")
            };
            Int32 o = (Int32)query.ExecuteScalar();
            if (o > 0) return true;
            return false;

        }


        public TR Get<TR>(IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;
            return query.Get<TR>();
        }

        public TR Get<TR>(String sql, IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            return query.Get<TR>(sql);

        }

        public TR Get<TR>(String commandText, CommandType commandType, IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);

            return query.Get<TR>(commandText, commandType);

        }



        public IList<TR> List<TR>(IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;

            return query.List<TR>();

        }

        public IList<TR> List<TR>(String sql, IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);

            return query.List<TR>(sql);

        }

        public IList<TR> List<TR>(String commandText, CommandType commandType, IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);

            return query.List<TR>(commandText, commandType);

        }

        public Int32 GetCount<TR>(IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.OrderBy = orderBy;
            IPager pager = query.CreatePager();
            return pager.GetCount();
        }

        public IList<TR> List<TR>(Int32 pageIndex, IDataSession session) where TR : class
        {

            ObjectMap om = ObjectMapContainer.Instance.Get(typeof(TR));
            this.table = om.Table;


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            IPager pager = query.CreatePager(this.size);
            return pager.List<TR>(pageIndex);

        }


    }

    public class QueryOperate<T1, T2>
        where T1 : class
        where T2 : class
    {
        public static QueryOperate<T1, T2> Create()
        {
            QueryOperate<T1, T2> qo = new QueryOperate<T1, T2>();
            return qo;
        }

        public static QueryOperate<T1, T2> Create(String sessionKey)
        {
            QueryOperate<T1, T2> qo = new QueryOperate<T1, T2>(sessionKey);
            return qo;
        }


        public QueryOperate() { }

        public QueryOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }


        private Table table;

        private Table fromTable;

        public QueryOperate<T1, T2> From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        private Expression expression;

        public QueryOperate<T1, T2> Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        private FieldAdapter[] select;

        public QueryOperate<T1, T2> Select(params FieldAdapter[] select)
        {
            this.select = select;
            return this;
        }

        private FieldAdapter[] orderBy;

        public QueryOperate<T1, T2> OrderBy(params FieldAdapter[] orderBy)
        {
            this.orderBy = orderBy;
            return this;
        }

        private FieldAdapter[] groupBy;

        public QueryOperate<T1, T2> GroupBy(params FieldAdapter[] groupBy)
        {
            this.groupBy = groupBy;
            return this;
        }

        private Int32 top;

        public QueryOperate<T1, T2> Top(Int32 top)
        {
            this.top = top;
            return this;
        }

        private Int32 size;

        public QueryOperate<T1, T2> Page(Int32 size)
        {
            this.size = size;
            return this;
        }

        public Combined<T1, T2> Get()
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                session.Open();
                return query.Get<T1, T2>();
            }
        }

        public Combined<T1, T2> Get(String sql)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.Get<T1, T2>(sql);
            }
        }

        public Combined<T1, T2> Get(String commandText, CommandType commandType)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.Get<T1, T2>(commandText, commandType);
            }
        }

        public IList<Combined<T1, T2>> List()
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                session.Open();
                return query.List<T1, T2>();
            }
        }

        public IList<Combined<T1, T2>> List(String sql)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.List<T1, T2>(sql);
            }
        }

        public IList<Combined<T1, T2>> List(String commandText, CommandType commandType)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.List<T1, T2>(commandText, commandType);
            }
        }

        public Int32 GetCount()
        {


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                IPager pager = query.CreatePager();
                session.Open();
                return pager.GetCount();
            }
        }

        public IList<Combined<T1, T2>> List(Int32 pageIndex)
        {


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                IPager pager = query.CreatePager(this.size);
                session.Open();
                return pager.List<T1, T2>(pageIndex);
            }
        }


        public Boolean Exist()
        {


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create("Count(*)")
                };
                session.Open();
                Int32 o = (Int32)query.ExecuteScalar();
                if (o > 0) return true;
                return false;

            }
        }

        public Boolean Exist(IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.Selects = new FieldAdapter[]{
                FieldAdapter.Create("Count(*)")
            };
            Int32 o = (Int32)query.ExecuteScalar();
            if (o > 0) return true;
            return false;

        }


        public Combined<T1, T2> Get(IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;
            return query.Get<T1, T2>();
        }

        public Combined<T1, T2> Get(String sql, IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            return query.Get<T1, T2>(sql);

        }

        public Combined<T1, T2> Get(String commandText, CommandType commandType, IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);

            return query.Get<T1, T2>(commandText, commandType);

        }



        public IList<Combined<T1, T2>> List(IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;

            return query.List<T1, T2>();

        }

        public IList<Combined<T1, T2>> List(String sql, IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);

            return query.List<T1, T2>(sql);

        }

        public IList<Combined<T1, T2>> List(String commandText, CommandType commandType, IDataSession session)
        {



            IQuery query = session.CreateQuery(Table);

            return query.List<T1, T2>(commandText, commandType);

        }

        public Int32 GetCount(IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.OrderBy = orderBy;
            IPager pager = query.CreatePager();
            return pager.GetCount();
        }

        public IList<Combined<T1, T2>> List(Int32 pageIndex, IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            IPager pager = query.CreatePager(this.size);
            return pager.List<T1, T2>(pageIndex);

        }



    }

    public class QueryOperate<T1, T2, T3>
        where T1 : class
        where T2 : class
        where T3 : class
    {
        public static QueryOperate<T1, T2, T3> Create()
        {
            QueryOperate<T1, T2, T3> qo = new QueryOperate<T1, T2, T3>();
            return qo;
        }

        public static QueryOperate<T1, T2, T3> Create(String sessionKey)
        {
            QueryOperate<T1, T2, T3> qo = new QueryOperate<T1, T2, T3>(sessionKey);
            return qo;
        }


        public QueryOperate() { }

        public QueryOperate(String sessionKey)
        {
            this._SessionKey = sessionKey;
        }

        private String _SessionKey = "";

        public String SessionKey
        {
            get { return this._SessionKey; }
            set { this._SessionKey = value; }
        }


        private Table table;

        private Table fromTable;

        public QueryOperate<T1, T2, T3> From(Table table)
        {
            this.fromTable = table;
            return this;
        }

        private Table Table
        {
            get
            {
                return this.fromTable == null ? this.table : fromTable;
            }
        }

        private Expression expression;

        public QueryOperate<T1, T2, T3> Where(Expression expression)
        {
            this.expression = expression;
            return this;
        }

        private FieldAdapter[] select;

        public QueryOperate<T1, T2, T3> Select(params FieldAdapter[] select)
        {
            this.select = select;
            return this;
        }

        private FieldAdapter[] orderBy;

        public QueryOperate<T1, T2, T3> OrderBy(params FieldAdapter[] orderBy)
        {
            this.orderBy = orderBy;
            return this;
        }

        private FieldAdapter[] groupBy;

        public QueryOperate<T1, T2, T3> GroupBy(params FieldAdapter[] groupBy)
        {
            this.groupBy = groupBy;
            return this;
        }

        private Int32 top;

        public QueryOperate<T1, T2, T3> Top(Int32 top)
        {
            this.top = top;
            return this;
        }

        private Int32 size;

        public QueryOperate<T1, T2, T3> Page(Int32 size)
        {
            this.size = size;
            return this;
        }

        public Combined<T1, T2, T3> Get()
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                session.Open();
                return query.Get<T1, T2, T3>();
            }
        }

        public Combined<T1, T2, T3> Get(String sql)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.Get<T1, T2, T3>(sql);
            }
        }

        public Combined<T1, T2, T3> Get(String commandText, CommandType commandType)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.Get<T1, T2, T3>(commandText, commandType);
            }
        }

        public IList<Combined<T1, T2, T3>> List()
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                query.Top = top;
                session.Open();
                return query.List<T1, T2, T3>();
            }
        }

        public IList<Combined<T1, T2, T3>> List(String sql)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.List<T1, T2, T3>(sql);
            }
        }

        public IList<Combined<T1, T2, T3>> List(String commandText, CommandType commandType)
        {

            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                session.Open();
                return query.List<T1, T2, T3>(commandText, commandType);
            }
        }

        public Int32 GetCount()
        {


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                IPager pager = query.CreatePager();
                session.Open();
                return pager.GetCount();
            }
        }

        public IList<Combined<T1, T2, T3>> List(Int32 pageIndex)
        {


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.GroupBy = groupBy;
                query.OrderBy = orderBy;
                query.Selects = select;
                IPager pager = query.CreatePager(this.size);
                session.Open();
                return pager.List<T1, T2, T3>(pageIndex);
            }
        }


        public Boolean Exist()
        {


            using (IDataSession session = new DataSession(this._SessionKey))
            {
                IQuery query = session.CreateQuery(Table);
                query.Expressions = expression;
                query.Selects = new FieldAdapter[]{
                    FieldAdapter.Create("Count(*)")
                };
                session.Open();
                Int32 o = (Int32)query.ExecuteScalar();
                if (o > 0) return true;
                return false;

            }
        }

        public Boolean Exist(IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.Selects = new FieldAdapter[]{
                FieldAdapter.Create("Count(*)")
            };
            Int32 o = (Int32)query.ExecuteScalar();
            if (o > 0) return true;
            return false;

        }


        public Combined<T1, T2, T3> Get(IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;
            return query.Get<T1, T2, T3>();
        }

        public Combined<T1, T2, T3> Get(String sql, IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);
            return query.Get<T1, T2, T3>(sql);

        }

        public Combined<T1, T2, T3> Get(String commandText, CommandType commandType, IDataSession session)
        {


            IQuery query = session.CreateQuery(Table);

            return query.Get<T1, T2, T3>(commandText, commandType);

        }



        public IList<Combined<T1, T2, T3>> List(IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            query.Top = top;

            return query.List<T1, T2, T3>();

        }

        public IList<Combined<T1, T2, T3>> List(String sql, IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);

            return query.List<T1, T2, T3>(sql);

        }

        public IList<Combined<T1, T2, T3>> List(String commandText, CommandType commandType, IDataSession session)
        {



            IQuery query = session.CreateQuery(Table);

            return query.List<T1, T2, T3>(commandText, commandType);

        }

        public Int32 GetCount(IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.OrderBy = orderBy;
            IPager pager = query.CreatePager();
            return pager.GetCount();
        }

        public IList<Combined<T1, T2, T3>> List(Int32 pageIndex, IDataSession session)
        {

            IQuery query = session.CreateQuery(Table);
            query.Expressions = expression;
            query.GroupBy = groupBy;
            query.OrderBy = orderBy;
            query.Selects = select;
            IPager pager = query.CreatePager(this.size);
            return pager.List<T1, T2, T3>(pageIndex);

        }



    }
}
